package com.tuyou.springboot.mybatis.controller;

import com.tuyou.springboot.comon.po.User;
import com.tuyou.springboot.mybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class UserController {

    @Resource
    UserService userService;

    @GetMapping("/user/mget/{id}")
    public String getUsers(@PathVariable("id") String id) {
        System.out.println(id);
        List<User> user = userService.getUserByIds(id.split(","));
        return user.toString();
    }

    @GetMapping("/user/get/{id}")
    public String getUser(@PathVariable("id") Integer id) {
        System.out.println(id);
        User user = userService.getUserById(id);
        return user.toString();
    }

    @GetMapping("/user/all")
    public String getAll() {
        List<User> allUser = userService.getAllUser();
        return allUser.toString();
    }

    @PostMapping("/user/save")
    public String saveUser(@RequestBody User user) {
        userService.saveUser(user);
        return "ok";
    }

    @PutMapping("/user/update")
    public String updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return "ok";
    }

    @DeleteMapping("user/del/{id}")
    public String delUser(@PathVariable("id") Integer id) {
        userService.delUser(id);
        return "ok";
    }

    @Transactional
    @GetMapping("user/multi")
    public String multiTransaction() {
        User user = new User("涂有", (short) 28);
        userService.saveUser(user);
//        userService.delUser(user.getId());
        return "success";
    }
}